<template>
  <div class="turn-out">
    <van-nav-bar title="转出" left-arrow @click-left="goBack"/>
    <div class="content">
      <div class="out">
        <label for="balance">转出数量</label>
        <input v-model="useMoney" type="number" :placeholder="money" id="balance">
        <span>{{getName}}</span>
        <span @click="turnAllMony">全部</span>
      </div>
    </div>
    <div class="confirm">
      <button @click="goTo" type="submit" class="btn">确认</button>
    </div>
    <div class="explain">
      <div class="title">转出说明</div>
      <div class="con">
        <p>1、当日12:00前转出，当日不计算收益，预计当日23:59前可到账；</p>
        <p>2、当日12:00后转出，当日仍计算收益，预计T+1日23:59前可到账；</p>
        <p>3、转出暂不收取任何费用；</p>
      </div>
    </div>
    <!-- 交易密码校验 -->
    <!-- 交易密码校验 -->
    <div class="cover" v-if="flagCover">
      <div class="cover-contain">
        <p style="text-align:center;font-weight:bold;font-size:16px;">请输入交易密码</p>
        <p>
          <input v-model="myPass" type="text" placeholder="请输入交易密码" name id>
        </p>
        <p style="font-size:10px;" @click="goToSet">忘记密码</p>
        <div class="submit">
          <button @click="checkPass" style="margin-top:0" type="submit" class="submit-btn">确定</button>
        </div>
        <img @click="flagCover=false" id="close" src="../../../assets/images/close.png" alt>
      </div>
    </div>
  </div>
</template>
<script>
import Vue from "vue";
import axios from "axios";
import {
  coinAssets,
  checkPassword,
  currentCoinPlanWithdraw,
  errorCount
} from "../../../axios/api.js";
import { NavBar, Toast } from "vant";
export default {
  components: {
    [NavBar.name]: NavBar,
    [Toast.name]: Toast
  },
  data() {
    return {
      myPass: "",
      flagCover: false,
      useMoney: "",
      getName: this.$route.query.name,
      money: "可转出余额为" + this.$route.query.money,
      allMoney: this.$route.query.money
    };
  },
  created() {
    console.log(this.money);
  },
  watch: {
    $route(to, from) {
      if (this.$route.params.id) {
        console.log(this.$route);
      }
    }
  },
  methods: {
    //忘记交易密码
    goToSet() {
      this.$router.push({ name: "passwordSetting" });
    },
    //返回上一页
    goBack() {
      this.$router.go(-1);
    },
    turnAllMony() {
      this.useMoney = this.$route.query.money;
    },
    //确认
    goTo() {
      var that = this;
      console.log(that.allMoney);
      console.log(that.useMoney);
      if (that.useMoney <= 0 || that.useMoney > that.allMoney) {
        Toast("交易数量有误！");
        return false;
      }

      errorCount({
        params: {
          token: localStorage.getItem("setToken")
        }
      }).then(res => {
        console.log(res.data, 999);
        if (res.data.code == 200) {
          that.flagCover = true;
        } else {
          Toast(res.data.msg);
          return false;
        }
      });
    },
    //校验交易密码
    checkPass() {
      var that = this;
      if (that.myPass == "") {
        Toast("请输入交易密码！");
        return false;
      }
      if(!/^\d{6}$/.test(that.myPass)){
         Toast('交易密码格式有误！')
        return false; 
      }
      checkPassword({
        params: {
          token: localStorage.getItem("setToken"),
          tradePassword: that.myPass
        }
      }).then(res => {
        if (res.data.code == 200) {
          console.log(res.data);
          that.turnInMoney();
        } else {
          Toast('交易密码错误！')
          that.flagCover = false;
        }
      });
    },
    //转出
    turnInMoney() {
      var that = this;
      $.ajax({
        type: "post",
        dataType: "json",
        data: {
          token: localStorage.getItem("setToken"),
          userCurrentCoinPlanId: that.$route.query.id1,
          virtualWalletId: that.$route.query.id2,
          transferOutAmount: that.useMoney
        },
        url: window.nowHref + "/userCurrentCoinPlan/currentCoinPlanWithdraw",
        success: function(data) {
          console.log(data);
          if (data.code == 200) {
            Toast("转出成功！");
            that.$router.push({ name: "product" });
          }
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.turn-out {
  .content {
    border-bottom: 1px solid #ebedf0;
    .out {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      padding: 10px 15px;
      text-align: left;
      font-size: 14px;
      color: #323233;
      label {
        margin-right: 14px;
      }
      input {
        width: 50%;
        flex: 1;
        border: 0;
      }
      span {
        width: 10%;
        margin-left: 10px;
        color: #d1a658;
      }
    }
  }
  .confirm {
    padding: 35px 15px;
    font-size: 14px;
    color: #fff;
    .btn {
      padding: 10px;
      width: 100%;
      height: 1.07rem;
      border: 0;
      border-radius: 3px;
      background: #d1a658;
      outline: none;
    }
  }
  .explain {
    padding: 0 15px;
    text-align: left;
    font-size: 12px;
    color: #969799f5;
    .title {
      padding-bottom: 10px;
      font-size: 14px;
      color: #323233;
    }
    .con {
      p {
        padding-bottom: 10px;
      }
    }
  }
  .cover {
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background-color: rgba(0, 0, 0, 0.4);
    z-index: 99;
    font-size: 14px;
    .cover-contain {
      width: 80%;
      background-color: #fff;
      border-radius: 5px;
      margin: 30vh auto;
      padding: 20px 20px 20px;
      box-sizing: border-box;
      position: relative;
      #close {
        position: absolute;
        right: 7px;
        top: 4px;
        width: 7%;
      }
      p {
        text-align: left;
        margin-bottom: 20px;
        input {
          border: none;
        }
      }
    }
  }
}
.submit {
  margin-top: 0.67rem;
  width: 100%;
  background-color: #fff;
  color: #fff;
  font-size: 14px;
  .submit-btn {
    width: 85%;
    height: 34px;
    text-align: center;
    line-height: 34px;
    font-size: 14px;
    background-color: #d6ab6f;
    color: white;
    border-radius: 20px;
    margin: 10vh auto 0;
    border: 0;
  }
}
</style>
